<?php
class Node_model extends Model {
	/**
	 * 添加节点
	 * @param unknown_type $params
	 */
	public function create($params) {
		$dNode = new Node_data();
		
		return $dNode->create($params);
	}
	
	/**
	 * 删除节点
	 * @param unknown_type $node
	 */
	public function delNode($params) {
		$dNode = new Node_data();
		
		return $dNode->delNode($params);
	}
	
	/**
	 * 更新节点信息
	 * @param unknown_type $nodeid
	 * @param unknown_type $params
	 */
	public function editNode($nodeId, $cols) {
		$dNode = new Node_data();
		
		$params = array('node_id' => $nodeId);
		return $dNode->editNode($params, $cols);
	}

	/**
	 * 获取节点
	 * @param unknown_type $params
	 */
	public function nodeInfo($params = array()) {
		$dNode = new Node_data();
		
		return $dNode->nodeInfo($params);
	}
	/**
	 * 获取全部节点
	 * @param unknown_type $params
	 */
	public function nodeAll($params = array()) {
		$dNode = new Node_data();
		
		return $dNode->nodeAll($params);
	}
	/**
	 * 获取节点信息
	 * @param unknown_type $nodeid
	 */
	public function nodeInfoById($nodeId) {
		$dNode = new Node_data();
		
		$params['node_id'] = $nodeId;
		return $dNode->nodeInfo($params);
	}
	/**
	 * 分页获取节点
	 * @param unknown_type $page
	 * @param unknown_type $size
	 * @param unknown_type $params
	 */
	public function page($page = 1, $size = 20, $params = array()) {
		$dNode = new Node_data();
		$num = $dNode->count($params);
		if($num === false) return false;
		if($num == 0) return array('num' => $num, 'data' => array(), 'page' => $page, 'size' => $size);
		
		$data = $dNode->page($page, $size, $params = array(), $orderby = '`node_id` DESC');
		if($data === false) return false;
		
		//获取节点分组信息
		$gid = array();
		foreach ($data as $key => $value) {
			$gid[] = $value['gid'];
		}
		$result = $this->groupInfoByIds($gid);
		if(!$result) return false;
		foreach ($data as $key => $value) {
			$data[$key]['group'] = $result[$value['gid']];
			unset($data[$key]['gid']);
		}
		return array('num' => $num, 'data' => $data, 'page' => $page, 'size' => $size);
	}
	
//节点分组-------------------------------------------------------------------------------------
	
	/**
	 * 添加节点分组
	 * @param unknown_type $params
	 */
	public function createGroup($params) {
		$dNode = new Node_data();
		
		return $dNode->createGroup($params);
	}
	
	/**
	 * 删除节点分组
	 * @param unknown_type $gid
	 */
	public function delGroup($params) {
		$dNode = new Node_data();
		
		return $dNode->delGroup($params);
	}
	
	/**
	 * 更新节点信息
	 * @param unknown_type $gid
	 * @param unknown_type $params
	 */
	public function editGroup($gid, $cols) {
		$dNode = new Node_data();
		
		$params = array('gid' => $gid);
		return $dNode->editGroup($params, $cols);
	}

	/**
	 * 节点分组信息
	 * @param unknown_type $gid
	 */
	public function groupInfoByGid($gid) {
		$dNode = new Node_data();
		
		$params = array('gid' => $gid);
		return $dNode->groupInfo($params);
	}
	/**
	 * 获取分组信息
	 * @param unknown_type $params
	 */
	public function groupInfo($params = array()) {
		$dNode = new Node_data();
		
		return $dNode->groupsInfo($params);
	}
	/**
	 * 批量获取分组信息
	 * @param unknown_type $gids
	 */
	public function groupInfoByIds($gids) {
		$dNode = new Node_data();
		
		$params['gid in'] = $gids;
		$result = $dNode->groupsInfo($params);
		if(!$result) return false;
		
		$return = array();
		foreach($result as $key => $value) {
			$return[$value['gid']] = $value;
		}
		return $return;
	}
	/**
	 * 获取所有分组
	 * @param unknown_type $params
	 */
	public function groupAll($params = array()) {
		$dNode = new Node_data();
		return $dNode->groupsInfo($params);
	}
	/**
	 * 分页获取节点分组
	 * @param unknown_type $page
	 * @param unknown_type $size
	 * @param unknown_type $params
	 */
	public function pageGroup($page = 1, $size = 20, $params = array()) {
		$dNode = new Node_data();
		$num = $dNode->countGroup($params);
		if($num === false) return false;
		if($num == 0) return array('num' => $num, 'data' => array(), 'page' => $page, 'size' => $size);
		
		$data = $dNode->pageGroup($page, $size, $params = array(), $orderby = '`gid` DESC');
		if($data === false) return false;
		
		//获取父分类
		$pid = array();
		foreach ($data as $key => $value) {
			if($value['pid'])$pid[] = $value['pid'];
		}
		if($pid) {
			$result = $this->groupInfoByIds($pid);
			if(!$result) return false;
			
			foreach ($data as $key => $value) {
				if(isset($result[$value['pid']])) $data[$key]['pgroup'] = $result[$value['pid']];
			}
		}
		return array('num' => $num, 'data' => $data, 'page' => $page, 'size' => $size);
	}
}